package com.jdz.gxq.info.admin.project.service;

import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.project.bo.Project;
import com.jdz.gxq.info.admin.project.bo.SignedProject;

public class SignedProjectServiceImpl implements SignedProjectService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * SignedProject检索分页用，查询记录条数
	 * @return
	 */
	public int getSignedProjectsCount(String projectName) {
		
		int totalCount = queryDAO.executeForObject("signedproject.getSignedProjectsCount",
				projectName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * SignedProject检索
	 * @return
	 */
	public List<SignedProject> getSignedProjects(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("signedproject.getSignedProjects", params);
	}
	
	/**
	 * 追加SignedProject
	 * @param signedproject
	 */
	public void insertSignedProject(SignedProject signedproject) {
		
		// 将项目状态变成：已签约
		signedproject.setProjectStatus("2");
		
		updateDAO.execute("signedproject.updateProject", signedproject);
		
		updateDAO.execute("signedproject.insertSignedProject", signedproject);
	}
	
	/**
	 * 更新SignedProject
	 * @param signedproject
	 */
	public void updateSignedProject(SignedProject signedproject) {
		updateDAO.execute("signedproject.updateSignedProject", signedproject);
	}
	
	/**
	 * 通过id得到signedproject
	 * @param id
	 * @return
	 */
	public SignedProject getSignedProjectById(int projectId) {

		SignedProject signedproject = queryDAO.executeForObject("signedproject.getSignedProjectById",
				projectId, SignedProject.class);
		
		return signedproject;
	}
	
	/**
	 * 删除SignedProject
	 * @param signedproject
	 */
	public void deleteSignedProject(SignedProject signedProject) {
			
		// 将原来的签约项目变为：在谈项目
		updateDAO.execute("signedproject.updateProject", signedProject);
		
		updateDAO.execute("signedproject.deleteSignedProjectById", signedProject.getProjectId());
	}
	
	/**
	 * 验证projectName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkSignedProjectName(SignedProject pSignedProject, int flag) {
		
		Project project = queryDAO.executeForObject("signedproject.getProjectByName",
				pSignedProject.getProjectName(), Project.class);

		if (project == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (project.getProjectId() == pSignedProject.getProjectId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}	
}
